﻿@using XinYue.Permission.Mode;
@{
    Layout = null;
    var parentList = ViewBag.parentList as List<Menu>;
    var sonList = ViewBag.sonList as List<Menu>;
    var functionList = ViewBag.functionList as List<Function>;
    var moduleList = ViewBag.moduleList as List<Module>;
    var moduleRole = ViewBag.moduleRole as ModuleRole;
    var role = ViewBag.role as Role;
    var roleFunctionList = ViewBag.roleFunctionList as List<RoleFunctionPermission>;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link href="~/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/Content/style.css" />
    <link href="~/assets/css/codemirror.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/assets/css/ace.min.css" />
    <link rel="stylesheet" href="~/font/css/font-awesome.min.css" />
    <!--[if lte IE 8]>
      <link rel="stylesheet" href="~/assets/css/ace-ie.min.css" />
    <![endif]-->
    <script src="~/Scripts/jquery-1.9.1.min.js"></script>
    <script src="~/assets/js/bootstrap.min.js"></script>
    <script src="~/assets/js/typeahead-bs2.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.min.js"></script>
    <script src="~/assets/js/jquery.dataTables.bootstrap.js"></script>
    <script src="~/assets/layer/layer.js" type="text/javascript"></script>
    <script src="~/assets/laydate/laydate.js" type="text/javascript"></script>
    <script src="~/Scripts/dragDivResize.js" type="text/javascript"></script>
    <title>编辑权限</title>
</head>

<body>
    <div class="Competence_add_style clearfix">
        <div class="left_Competence_add">
            <div class="title_name">编辑权限</div>
            <div class="Competence_add">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限名称 </label>
                    <input type="hidden" value="@role.Id" id="role" />
                    <div class="col-sm-9"><input type="text" id="Name" value="@role.Name" placeholder="" name="权限名称" class="col-xs-10 col-sm-5" /></div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="form-field-1"> 权限描述 </label>
                    <div class="col-sm-9"><textarea name="权限描述" class="form-control" id="Remark" placeholder="" onkeyup="checkLength(this);">@role.Remark</textarea><span class="wordage">剩余字数：<span id="sy" style="color:Red;">200</span>字</span></div>
                </div>
                <div class="form-group">
                    <input id="mid" type="hidden" value="@moduleRole.ModuleId" />
                    <select id="module" style="margin-left:5%">
                        @for (int i = 0; i < moduleList.Count; i++)
                        {
                            <option value=@moduleList[i].Id>@moduleList[i].Name</option>
                        }
                    </select>
                </div>
                <!--按钮操作-->
                <div class="Button_operation">
                    <input id="commit" class="btn btn-primary radius" type="button" value="保存并提交" />
                    @*<button onclick="article_save_submit();" class="btn btn-primary radius" type="submit"><i class="fa fa-save "></i> 保存并提交</button>*@
                    <button onclick="article_save();" class="btn btn-secondary  btn-warning" type="button"><i class="fa fa-reply"></i> 返回上一步</button>
                    <button onclick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
                </div>
            </div>
        </div>
        <!--权限分配-->
        <div class="Assign_style">
            <div class="title_name">权限分配</div>
            <div class="Select_Competence">
                @for (int i = 0; i < parentList.Count; i++)
                {
                    <dl class="permission-list" value="@parentList[i].ModuleId">
                        <dt><label class="middle"><input name="user-Character-0" class="ace" type="checkbox" id="id-disable-check" /><span class="lbl">@parentList[i].Name</span></label></dt>
                        <dd>
                            @for (int j = 0; j < sonList.Count; j++)
                            {
                                if (sonList[j].ParentId == parentList[i].Id)
                                {
                                    <dl class="cl permission-list2">
                                        @if (roleFunctionList.Count != 0)
                                        {
                                            for (int g = 0; g < roleFunctionList.Count; g++)
                                            {
                                                if (roleFunctionList[g].MenuId == sonList[j].Id)
                                                {
                                                    <dt><label class="middle"><input type="checkbox" checked=checked value=@sonList[j].Id class="ace" name="user-Character-0-0" id="id-disable-check" /><span class="lbl">@sonList[j].Name</span></label></dt>
                                                    break;
                                                }
                                                else if (g == roleFunctionList.Count - 1)
                                                {
                                                    <dt><label class="middle"><input type="checkbox" value=@sonList[j].Id class="ace" name="user-Character-0-0" id="id-disable-check" /><span class="lbl">@sonList[j].Name</span></label></dt>
                                                }
                                            }
                                        }
                                        else
                                        {
                                            <dt><label class="middle"><input type="checkbox" value=@sonList[j].Id class="ace" name="user-Character-0-0" id="id-disable-check" /><span class="lbl">@sonList[j].Name</span></label></dt>
                                        }
                                        <dd>
                                            @for (int k = 0; k < functionList.Count; k++)
                                            {
                                                if (functionList[k].MenuId == sonList[j].Id)
                                                {
                                                    if (roleFunctionList.Count != 0)
                                                    {
                                                        for (int u = 0; u < roleFunctionList.Count; u++)
                                                        {
                                                            if (roleFunctionList[u].FunctionId == functionList[k].Id)
                                                            {
                                                                <label class="middle"><input type="checkbox" checked=checked value=@functionList[k].Id class="ace" name="user-Character-0-0-0" id="user-Character-0-0-0" /><span class="lbl">@functionList[k].Name</span></label>
                                                                break;
                                                            }
                                                            else if (u == roleFunctionList.Count - 1)
                                                            {
                                                                <label class="middle"><input type="checkbox" value=@functionList[k].Id class="ace" name="user-Character-0-0-0" id="user-Character-0-0-0" /><span class="lbl">@functionList[k].Name</span></label>
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        <label class="middle"><input type="checkbox" value=@functionList[k].Id class="ace" name="user-Character-0-0-0" id="user-Character-0-0-0" /><span class="lbl">@functionList[k].Name</span></label>
                                                    }
                                                }
                                            }
                                        </dd>
                                    </dl>
                                }
                            }
                        </dd>
                    </dl>
                }
            </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    $(function () {
        var key = $("#mid").val();
        //根据值让option选中
        $("#module option[value='" + key + "']").attr("selected", "selected");
        var moId = $("#module").val();
        var module = $(".permission-list");
        module.each(function (i, dom) {
            if ($(this).attr("value") != moId) {
                $(this).hide();
            }
            else {
                $(this).show();
            }
        });

        //下拉框选中内容改变事件
        $("#module").change(function () {
            var moId = $("#module").val();
            var module = $(".permission-list");
            module.each(function (i, dom) {
                if ($(this).attr("value") != moId) {
                    $(this).hide();
                    $("input[type='checkbox']").each(function (i, dom) {
                        $(this).prop("checked", false);
                    });
                }
                else {
                    $(this).show();
                    $("input[type='checkbox']").each(function (i, dom) {
                        $(this).prop("checked", false);
                    });
                }
            });
        });

        //提交按钮点击事件
        $("#commit").click(function () {
            //Ajax提交List集合
            var ids = [];
            var fun = $(".permission-list2 dd input:checkbox");
            fun.each(function (i, dom) {
                if ($(this).prop("checked")) {
                    var sonId = $(this).closest("dl").find("dt input:checkbox").val();
                    var funId = $(this).val();
                    ids.push({ MenuId: sonId, FunctionId: funId });
                }
            });
            var moId = $("#module").val();
            var role = {};
            role.Name = $("#Name").val();
            role.Remark = $("#Remark").val();
            var id = $("#role").val();
            var data = {};
            data.role = role;
            data.roleFunctionList = ids;
            data.ModuleId = moId;
            $.ajax({
                url: "/Role/UpdateRole?id=" + id,
                type: "post",
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: "application/json",
                success: function (result) {
                    if (result.Success) {
                        layer.msg('修改成功', {
                            title: '提示框',
                            icon: 0,
                            time: 2000
                        }, function () {
                            window.location.href = "/Home/Admin_Competence";
                        });
                        layer.close(index);
                    }
                    else {
                        layer.msg('修改有误', {
                            title: '提示框',
                            icon: 0,
                            time: 1000
                        });
                    }
                }
            });
        });
    });

    //初始化宽度、高度
    $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();;
    $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
    $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
    //当文档窗口发生改变时 触发
    $(window).resize(function () {

        $(".Assign_style").width($(window).width() - 500).height($(window).height()).val();
        $(".Select_Competence").width($(window).width() - 500).height($(window).height() - 40).val();
        $(".left_Competence_add,.Competence_add_style").height($(window).height()).val();;
    });
    /*字数限制*/
    function checkLength(which) {
        var maxChars = 200; //
        if (which.value.length > maxChars) {
            layer.open({
                icon: 2,
                title: '提示框',
                content: '您出入的字数超多限制!',
            });
            // 超过限制的字数了就将 文本框中的内容按规定的字数 截取
            which.value = which.value.substring(0, maxChars);
            return false;
        } else {
            var curr = maxChars - which.value.length; //250 减去 当前输入的
            document.getElementById("sy").innerHTML = curr.toString();
            return true;
        }
    };
    /*按钮选择*/
    $(function () {
        $(".permission-list dt input:checkbox").click(function () {
            $(this).closest("dl").find("dd input:checkbox").prop("checked", $(this).prop("checked"));
        });
        $(".permission-list2 dd input:checkbox").click(function () {
            var l = $(this).parent().parent().find("input:checked").length;
            var l2 = $(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if ($(this).prop("checked")) {
                $(this).closest("dl").find("dt input:checkbox").prop("checked", true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
            }
            else {
                if (l == 0) {
                    $(this).closest("dl").find("dt input:checkbox").prop("checked", false);
                }
                if (l2 == 0) {
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                }
            }

        });
    });

</script>
